package com.mbf.crm.controller.system;

import javax.validation.Valid;

import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mbf.crm.common.AjaxResult;
import com.mbf.crm.common.DataGrid;
import com.mbf.crm.model.dao.ResourceDao;
import com.mbf.crm.model.domain.Resource;
import com.mbf.crm.service.ResourceService;

/**
 * 资源控制器
 * @author 晏鼠  张劲松
 *
 */
@Controller
@RequestMapping("/system/resource")
@Transactional(readOnly = true)
public class ResourceController {

	Logger logger = Logger.getLogger(ResourceController.class);
	
	@Autowired
	ResourceDao resourceDao;
	
	@Autowired
	private ResourceService resourceService;
	
	@RequestMapping
	public void index(){
	}
	
	@RequestMapping("/list")
	@ResponseBody
	public DataGrid<Resource> list(){
		return new DataGrid<Resource>(resourceDao.findAll(ResourceDao.WEIGHT_SORT));
	}
	
	@RequestMapping("/parent/tree")
	@ResponseBody
	public Iterable<Resource> parentTree(){
		return resourceService.getResourceTree();
	}
	
	@RequestMapping("form")
	public void form(Long id, Model model){
		if(id != null){
			ObjectMapper mapper = new ObjectMapper();
			Resource resource = resourceDao.findOne(id);
			try{
				model.addAttribute("resource",mapper.writeValueAsString(resource));
			}catch(JsonProcessingException e){
				logger.error("json转换错误",e);
			}
			if(resource.getParent() != null){
				model.addAttribute("parentId", resource.getParent().getId());
			}
		}
	}
	
	@RequestMapping({"/save","/update"})
	@Transactional
	@ResponseBody
	public Object save(@Valid Resource resource,BindingResult br){
		if(br.hasErrors()){
			logger.error("对象校验失败"+ br.getAllErrors());
			return new AjaxResult(false).setData(br.getAllErrors());
		}else{
			return resourceDao.save(resource);
		}
	}
	
	@RequestMapping("/delete")
	@Transactional
	@ResponseBody
	public AjaxResult delete(Long id){
		try{
			resourceDao.deleteById(id);
		}catch(Exception e){
			return new AjaxResult(false, "删除资源失败").setData(e.getMessage());
		}
		return new AjaxResult();
	}
	
}
